package game.helper;

import game.entity.AbsSprite;
import game.shared.Constants;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class Lightning implements IAbility {

	ShapeRenderer shapeRenderer;
	Camera camera;
	Rectangle viewport;

	int curDetail;
	Random r;

	public Lightning(Camera camera, Rectangle viewport,
			ShapeRenderer shapeRenderer) {
		this.camera = camera;
		this.viewport = viewport;
		this.shapeRenderer = shapeRenderer;
		curDetail = 3;
		r = new Random();
	}

	@Override
	public void action(AbsSprite sprite) {

	}

	@Override
	public void actionShape() {
		if (Gdx.input.isTouched()) {
			shapeRenderer.begin(ShapeType.Line);
			shapeRenderer.setColor(144, 254, 115, 1);
			drawLightning(-179, 66, Gdx.input.getX() - Constants.SCREEN_WIDTH / 2, -Gdx.input.getY() + Constants.SCREEN_HEIGHT / 2, 70);
			shapeRenderer.end();
		}
	}

	private void drawLightning(float x1, float y1, float x2, float y2,
			float display) {
		if (display < curDetail) {
			shapeRenderer.line(x1, y1, x2, y2);
		} else {
			float mid_x = (x2 + x1) / 2;
			float mid_y = (y2 + y1) / 2;
			mid_x += (r.nextFloat() - 0.3) * display;
			mid_y += (r.nextFloat() - 0.3) * display;
			drawLightning(x1, y1, mid_x, mid_y, display / 2);
			drawLightning(x2, y2, mid_x, mid_y, display / 2);
		}
	}
}
